<?php


namespace app\services\share;

use app\dao\share\SharePlatformDao;
use app\services\BaseServices;

/**
 * Class SharePlatformServices
 * @package app\services\share
 */
class SharePlatformServices extends BaseServices
{
    /**
     * SharePlatformServices constructor.
     * @param SharePlatformDao $dao
     */
    public function __construct(SharePlatformDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 获取列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @return array
     */
    public function getList(array $where, int $page = 0, int $limit = 0)
    {
        if (!$page && !$limit) {
            [$page, $limit] = $this->getPageValue();
        }
        $list = $this->dao->getList($where, $page, $limit);
        $count = $this->dao->count($where);
        return compact('list', 'count');
    }

    /**
     * 新增编辑
     * @param array $data
     * @return mixed
     */
    public function save(array $data)
    {
        $id = $data['id'];
        unset($data['id']);
        $info = $this->transaction(function () use ($id, $data) {
            if ($id) {
                $info = $this->dao->update($id, $data);
            } else {
                unset($data['id']);
                $data['add_time'] = time();
                $info = $this->dao->save($data);
            }
            return $info;
        });
        return $info;
    }

    /**
     * 删除商品
     * @param int $id
     */
    public function del(int $id)
    {
        return $this->dao->delete($id);
    }
}
